package com.neusoft.zqygbc.neusoft_wenda.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.SearchView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.adapter.CreateQuestionAdapter;
import com.neusoft.zqygbc.neusoft_wenda.adapter.QListAdapter;
import com.neusoft.zqygbc.neusoft_wenda.adapter.QuestionAnswerAdapter;
import com.neusoft.zqygbc.neusoft_wenda.base.RetrofitCallback;
import com.neusoft.zqygbc.neusoft_wenda.fragment.FindFragment;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.AnswersEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.QuestionEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.QuestionList;
import com.neusoft.zqygbc.neusoft_wenda.model.QusetionModel;
import com.neusoft.zqygbc.neusoft_wenda.view.SwipyRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class SearchActivity extends AppCompatActivity {
    private TextView btn_cancel;
    private SearchView searchview;
    SharedPreferences sid;
    private RecyclerView recyerview;
    private String search;
    private SwipyRefreshLayout refreshLayout;
    private LinearLayoutManager linearLayoutManager;
    private int refreshTime = 0;
    private int times = 0;
    private int page = 1;
    private final int TOP_REFRESH = 1;
    private final int BOTTOM_REFRESH = 2;
    private ArrayList<QuestionEntity> list = new ArrayList();
    private QuestionList result;
    private QListAdapter adapter;
    private QusetionModel questionModel;
    private Call call;
    private RetrofitCallback<List<QuestionEntity>> questionEntityRetrofitCallback;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initViews();
        initEvents();

    }


    void initViews() {
        searchview = (SearchView) findViewById(R.id.searchview);
        searchview.setSubmitButtonEnabled(true);
        btn_cancel= (TextView) findViewById(R.id.btn_search_cancel);
        sid = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        recyerview = (RecyclerView) findViewById(R.id.rv_search);
        recyerview.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
        adapter = new QListAdapter(SearchActivity.this, list);
        recyerview.setAdapter(adapter);//设置适配器
    }


    void initEvents() {
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                search = query;
                initData();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

    }


    void initData() {
        questionModel=new QusetionModel();
        call=questionModel.search(search);

        questionEntityRetrofitCallback=new RetrofitCallback<List<QuestionEntity>>() {
            @Override
            public void onResponse(List<QuestionEntity> questionEntities) {
                if(questionEntities!=null){
                    recyerview.setAdapter(new CreateQuestionAdapter(SearchActivity.this, questionEntities, R.layout.cardview_myquestion));
                }else {
                    Toast.makeText(SearchActivity.this, "没有搜索到内容", Toast.LENGTH_SHORT).show();
                }
            }
        };
        questionModel.bindCallback(call,questionEntityRetrofitCallback);




    }

}
